﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.ServiceUserSettings;

namespace LiveWebServiceAdmin
{
    public partial class admin : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Context.User.Identity.IsAuthenticated)
                lblCurrentUser.Text = this.Context.User.Identity.Name;
            else
                lblCurrentUser.Visible = false;

            SiteMap.SiteMapResolve +=new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie objCookie = new HttpCookie("LAemWS");
            Response.Cookies.Clear();
            Response.Cookies.Add(objCookie);
            objCookie.Values.Add("LAemWS", "");
            DateTime dtExpiry = DateTime.Now.AddDays(0);
            Response.Cookies["LAemWS"].Expires = dtExpiry;

            Response.Redirect("/login.aspx?JScript=1");
        }

        private SiteMapNode SiteMap_SiteMapResolve(Object sender, SiteMapResolveEventArgs e)
        {
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            if (currentNode.ParentNode != null)
            {
                string curuser = string.Empty;
                string curorg = string.Empty;
                string cursid = string.Empty;
                string curaid = string.Empty;

                bool IsAdmin = Settings.UserInfo.IsAdminUser(e.Context.User.Identity.Name);
                bool IsUser = Settings.UserInfo.IsServiceUser(e.Context.User.Identity.Name);

                if (IsAdmin)
                        curuser = e.Context.Request.QueryString["manageuser"];
                else if (IsUser)
                        curuser = e.Context.Request.QueryString["manageuser"];


                if (!string.IsNullOrEmpty(e.Context.Request.QueryString["manageorganization"]))
                    curorg = e.Context.Request.QueryString["manageorganization"];

                if (!string.IsNullOrEmpty(e.Context.Request.QueryString["managesite"]))
                    cursid = e.Context.Request.QueryString["managesite"];

                if (!string.IsNullOrEmpty(e.Context.Request.QueryString["manageaccounts"]))
                {
                    UserSettings usItem = new UserSettings(curuser);

                    curaid = usItem.GetAccount(cursid, e.Context.Request.QueryString["manageaccounts"]).domainAdm;
                }

                if (currentNode.Title == "Organizations")
                {
                    currentNode.ParentNode.Title = curuser;
                    currentNode.ParentNode.Url = "/users.aspx";
                }
                if (currentNode.Title == "Sites")
                {
                    currentNode.ParentNode.ParentNode.Title = curuser;
                    currentNode.ParentNode.ParentNode.Url = "/users.aspx";

                    currentNode.ParentNode.Title = curorg;
                    currentNode.ParentNode.Url = "/organizations.aspx?manageuser=" + curuser;
                }
                else if (currentNode.Title == "Accounts")
                {
                    currentNode.ParentNode.ParentNode.ParentNode.Title = curuser;
                    currentNode.ParentNode.ParentNode.ParentNode.Url = "/users.aspx";

                    currentNode.ParentNode.ParentNode.Title = curorg;
                    currentNode.ParentNode.ParentNode.Url = "/organizations.aspx?manageuser=" + curuser;

                    currentNode.ParentNode.Title = cursid;
                    currentNode.ParentNode.Url = "/siteids.aspx?manageuser=" + curuser + "&manageorganization=" + curorg;
                }
                else if (currentNode.Title == "Domains")
                {
                    currentNode.ParentNode.ParentNode.ParentNode.ParentNode.Title = curuser;
                    currentNode.ParentNode.ParentNode.ParentNode.ParentNode.Url = "/users.aspx";

                    currentNode.ParentNode.ParentNode.ParentNode.Title = curorg;
                    currentNode.ParentNode.ParentNode.ParentNode.Url = "/organizations.aspx?manageuser=" + curuser;

                    currentNode.ParentNode.ParentNode.Title = cursid;
                    currentNode.ParentNode.ParentNode.Url = "/siteids.aspx?manageuser=" + curuser + "&manageorganization=" + curorg;

                    currentNode.ParentNode.Title = curaid;
                    currentNode.ParentNode.Url = "/accounts.aspx?manageuser=" + curuser + "&manageorganization=" + curorg + "&managesite=" + cursid;
                }
            }

            return currentNode;
        }
    }
}
